﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word
{
    public sealed class LatentStyles : OpenXMLEntity
    {

        public LatentStyles()
        {
            this._Exceptions = new LatentStyleExceptionCollection(this);
        }

        private LatentStyleExceptionCollection _Exceptions = null;

        public LatentStyleExceptionCollection Exceptions
        {
            get
            {
                return _Exceptions;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool? DefLockedState
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public decimal? Count
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool? IsDefaultPrimaryStyle
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool? DefaultSemiHidden
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public decimal? DefaultUIPriority
        {
            get;
            set;
        }

        /// <summary>
        /// By Default un hide if used
        /// </summary>
        public bool? DefaultUnHide
        {
            get;
            set;
        }



        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement xLatent = new XElement(NS.WM + "latentStyles");

            if (this.DefLockedState != null)
                xLatent.Add(new XAttribute(NS.WM + "defLockedState",
                    this.DefLockedState.Value.ToOnOff()));

            if (this.Count != null)
                xLatent.Add(new XAttribute(NS.WM + "count", this.Count.Value));

            if (this.IsDefaultPrimaryStyle != null)
                xLatent.Add(new XAttribute(NS.WM + "defQFormat", this.IsDefaultPrimaryStyle.Value.ToOnOff()));

            if (this.DefaultSemiHidden != null)
                xLatent.Add(new XAttribute(NS.WM + "defSemiHidden", this.DefaultSemiHidden.Value.ToOnOff()));

            if (this.DefaultUIPriority != null)
                xLatent.Add(new XAttribute(NS.WM + "defUIPriority", this.DefaultUIPriority.Value));

            if (this.DefaultUnHide != null)
                xLatent.Add(new XAttribute(NS.WM + "defUnhideWhenUsed", this.DefaultUnHide.Value.ToOnOff()));

            if (this.Exceptions != null)
            {
                foreach (var c in this.Exceptions)
                {
                    xLatent.Add(c.ToXml());
                }
            }

            return xLatent;
        }

        private void ParseInternal(XElement latentStyles)
        {
            if (latentStyles.Name.LocalName != "latentStyles")
                throw new Exception("Invalid style node");

            var defLockState = latentStyles.Attribute(NS.WM + "defLockedState");

            if (defLockState != null)
            {
                this.DefLockedState = defLockState.Value.ToBoolean();
            }

            var count = latentStyles.Attribute(NS.WM + "count");

            if (count != null)
            {
                this.Count = Convert.ToDecimal(count.Value);
            }

            var defQFormat = latentStyles.Attribute(NS.WM + "defQFormat");

            if (defQFormat != null)
            {
                this.IsDefaultPrimaryStyle = defQFormat.Value.ToBoolean();
            }

            var defUiPriority = latentStyles.Attribute(NS.WM + "defUIPriority");

            if (defUiPriority != null)
            {
                this.DefaultUIPriority = Convert.ToDecimal(defUiPriority.Value);
            }

            var defUnHideWhenUsed = latentStyles.Attribute(NS.WM + "defUnhideWhenUsed");

            if (defUnHideWhenUsed != null)
            {
                this.DefaultUnHide = defUnHideWhenUsed.Value.ToBoolean();
            }

            var childs = latentStyles.Elements(NS.WM + "lsdException");

            foreach (var child in childs)
            {
                var newChild = LatentStyleException.Parse(child);

                if (newChild != null)
                    this.Exceptions.Add(newChild);
            }
        }

        public static LatentStyles Parse(XElement latentStyles)
        {
            LatentStyles st = new LatentStyles();
            st.ParseInternal(latentStyles);
            return st;
        }
    }
}
